package com.yinhai.ta404.module.hunan.userarea.rest;

import com.yinhai.ta404.component.org.core.autoconfigure.TaRootConfig;
import com.yinhai.ta404.component.org.core.service.IPermissionValidateService;
import com.yinhai.ta404.component.org.core.vo.TaUserConditionVo;
import com.yinhai.ta404.component.org.core.vo.TaUserVo;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.component.org.orguser.area.service.read.AreaManagementReadService;
import com.yinhai.ta404.component.org.orguser.user.sensitive.SensitiveOrgData;
import com.yinhai.ta404.component.org.orguser.user.service.read.UserManagementReadService;
import com.yinhai.ta404.component.org.sysmg.configfield.service.read.ConfigurableFieldReadService;
import com.yinhai.ta404.component.org.sysmg.configfield.vo.TaConfigurableFieldTableVo;
import com.yinhai.ta404.core.IConstants;
import com.yinhai.ta404.core.exception.AppException;
import com.yinhai.ta404.core.restservice.BaseRestService;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.utils.ObjectConversionUtil;
import com.yinhai.ta404.core.utils.StringUtils;
import com.yinhai.ta404.core.utils.ValidateUtil;
import com.yinhai.ta404.core.validate.annotation.V;
import com.yinhai.ta404.module.hunan.userarea.service.read.UserAreaReadService;
import com.yinhai.ta404.module.hunan.userarea.service.write.UserAreaWriteService;
import com.yinhai.ta404.module.hunan.userarea.vo.TaUserAreaNameVo;
import com.yinhai.ta404.module.hunan.userarea.vo.TaUserAreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Validated
@RestService({"org/orguser/userAreaRestService"})
public class UserAreaManagementRestService extends BaseRestService {
    private static final String AREA_NAME_SEPARATOR = ",";
    @Resource
    private IPermissionValidateService permissionValidateService;
    @Resource
    private UserManagementReadService userManagementReadService;
    @Autowired
    TaRootConfig rootConfig;
    @Resource
    private ConfigurableFieldReadService configurableFieldReadService;
    @Resource
    private UserAreaReadService userAreaReadService;
    @Resource
    private UserAreaWriteService userAreaWriteService;
    @Autowired
    private AreaManagementReadService areaManagementReadService;


    @RequestMapping({"queryUserByConditon"})
    @SensitiveOrgData
    public void queryUserByCondition(TaUserConditionVo condition, Boolean virtualOrg, PageParam pageParam) {
        condition.setCurUserId(this.getCurUserId());
        if (Boolean.TRUE.equals(virtualOrg)) {
            condition.setOrgId(this.rootConfig.getVirtualOrgId());
        }
        Page<TaUserVo> usersPage = this.userManagementReadService.queryUserByCondition(condition, pageParam);

        List<TaUserVo> taUserVos = usersPage.getList();
        List<TaUserAreaNameVo> returnVos = taUserVos.stream().map(taUserVo -> {
            return ObjectConversionUtil.convert(new TaUserAreaNameVo(this.userAreaReadService.queryAreaByUserId(taUserVo.getUserId()).stream().map(TaAreaPo::getAreaName).collect(Collectors.joining(AREA_NAME_SEPARATOR))), taUserVo);
        }).collect(Collectors.toList());


        Page<TaUserAreaNameVo> returnPage = new Page<>(returnVos);
        returnPage.setCurrentSize(usersPage.getCurrentSize());
        returnPage.setPageNum(usersPage.getPageNum());
        returnPage.setPageSize(usersPage.getPageSize());
        returnPage.setTotal(usersPage.getTotal());

        this.setPageBean(returnPage);
        TaConfigurableFieldTableVo taConfigurableFieldVo = new TaConfigurableFieldTableVo();
        taConfigurableFieldVo.setType("1");
        this.setData("configTable", this.configurableFieldReadService.selectAllManageableFields(taConfigurableFieldVo));
    }


    @RequestMapping({"queryUserAreaByUserId"})
    public void queryUserAreaByUserId(@V({"notnull"}) String userId) {
        if(permissionValidateService.checkUserPermission(userId, IConstants.YES)){
            throw new AppException("无权查询此人员信息");
        }
        List<TaUserAreaVo> userAreas = userAreaReadService.queryUserAreaByUserId(userId);
        this.setData("list", userAreas.stream().map(TaUserAreaVo::getAreaId).collect(Collectors.toList()));
    }


    @RequestMapping({"updateUserAreaByUserId"})
    public void updateUserAreaByUserId(@V({"notnull"}) String userId, String addAreaIds, String removeAreaIds) {
        if(permissionValidateService.checkUserPermission(userId, IConstants.YES)){
            throw new AppException("无权操作此人员信息");
        }
        userAreaWriteService.updateUserArea(userId, StringUtils.string2List(addAreaIds), StringUtils.string2List(removeAreaIds), getCurUserId());
    }


    @RequestMapping({"queryAreaByAsync"})
    public void queryAreaByAsync(String areaId) {

        if (ValidateUtil.isEmpty(areaId)) {
            this.setData("areaList", this.areaManagementReadService.queryArea(getCurUserAccount().getAreaId(), IConstants.YES));
        } else {
            this.setData("areaList", this.areaManagementReadService.queryAreaByAsync(areaId));
        }
    }
}
